﻿using System;
using System.Configuration;
using System.Linq;
using System.Web.Http;

namespace LuceneApiServer.Services
{
    public class Option
    {
        public bool Delfromindex = false;
        public string FromName;
        public string[] Ids;
        public string IndexName;
        public LnModel[] Models;
        public string UserName;
        public string KeyWord;
    }

    public enum Optionenum
    {
        SetIndex,
        DeleteIndex,
        DeleteIndexAll,
        OptimizeIndex,
        Merge
    }

    public class ApiServiceController : ApiController
    {
        public string Get()
        {
            return "Lucene.net server";
        }

        /// <summary>
        ///     搜索索引
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <returns></returns>
        [HttpPost]
        public LnPage SearchIndex([FromBody] LnQuery query)
        {
            return LnHelper.SearchIndex(query);
        }

        [HttpPost]
        public LnIdsPage SearchIndexReturnIds(string returnIds, [FromBody] LnQuery query)
        {
            return LnHelper.SearchIndexIds(query);
        }
        /// <summary>
        ///     增删改等操作
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        [HttpPost]
        public bool IndexOpeartion(string doOption, [FromBody] Option option)
        {
            if (doOption==null||option == null || option.UserName != ConfigurationManager.AppSettings.Get("Account") ||
                option.KeyWord != ConfigurationManager.AppSettings.Get("Password"))
                return false;
            Optionenum op;
            if (Enum.TryParse(doOption, out op))
            {
                switch (op)
                {
                    case Optionenum.SetIndex:
                        return SetIndex(option.IndexName, option.Models);
                    case Optionenum.DeleteIndex:
                        return DeleteIndex(option.IndexName, option.Ids);

                    case Optionenum.DeleteIndexAll:
                        return DeleteIndex(option.IndexName);

                    case Optionenum.OptimizeIndex:
                        return OptimizeIndex(option.IndexName);
                    case Optionenum.Merge:
                        return Merge(option.FromName, option.IndexName, option.Delfromindex);

                    default:
                        return false;
                }
            }
            return false;
        }
        #region ADC

        /// <summary>
        ///     创建或更新到索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="models">字段</param>
        /// <returns></returns>
        private bool SetIndex(string indexName, LnModel[] models)
        {
            if (models == null || models.Length == 0) return false;
            return LnHelper.SetIndex(indexName, models);
        }

        /// <summary>
        ///     删除索引的全部记录
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns></returns>
        private bool DeleteIndex(string indexName)
        {
            return LnHelper.DeleteIndex(indexName);
        }

        /// <summary>
        ///     删除索引的记录
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="ids">记录的id值</param>
        /// <returns></returns>
        private bool DeleteIndex(string indexName, string[] ids)
        {
            if (ids == null || ids.Length == 0) return false;
            var idFields = ids.Select(item => new LnField {Name = "ID", Value = item});
            return LnHelper.DeleteIndex(indexName, idFields);
        }

        /// <summary>
        ///     优化索引
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        private bool OptimizeIndex(string indexName)
        {
            return LnHelper.OptimizeIndex(indexName);
        }

        /// <summary>
        ///     合并索引
        /// </summary>
        /// <param name="fromIndex"></param>
        /// <param name="toIndex"></param>
        /// <param name="delfromindex"></param>
        private bool Merge(string fromIndex, string toIndex, bool delfromindex = false)
        {
            if (string.IsNullOrWhiteSpace(fromIndex) || string.IsNullOrWhiteSpace(toIndex)) return false;
            LnHelper.Merge(fromIndex, toIndex, delfromindex);
            return true;
        }
        #endregion
    }
}